package com.xjm.mid.compent.rabbitmq.web;

import com.xjm.mid.compent.rabbitmq.config.RabbitMQDelayConfig;
import com.xjm.mid.compent.rabbitmq.model.Letter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/delay")
    public void sendMsg(){
        Letter letter = new Letter();
        letter.setRecipient("福尔摩斯");
        letter.setContext("您有新的订单需要处理！");
        String expiredTime = "5000";
        rabbitTemplate.convertAndSend(RabbitMQDelayConfig.DELAY_EXCHANGE,RabbitMQDelayConfig.DELAY_ROUTINGKEY,letter,message->{
            message.getMessageProperties().setExpiration(expiredTime);
            return message;
        });
        log.info("[发送时间] - [{}]", LocalDateTime.now());
    }
}
